<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

class GetopenidController extends Controller

{

	public function getopenid()
    {
		$appid = "wxe4f29d24b9386589";  // 你的小程序的AppID - 微信小程序后台获得

    	$secret = "0ed49ef608cf301c8bfdd4dec956f465";  // 你的小程序的AppSecret - 微信小程序后台获得

    	$grant_type = "authorization_code";

        //获取微信小程序传过来的code
        // $js_code = $request['code'];
		$js_code = request::input('js_code');

        $curl = curl_init();

        //使用curl_setopt() 设置要获得url地址
        //  $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->appid . '&secret=' . $this->secret . '&js_code=' . $js_code . '&grant_type=' . $this->grant_type;

		$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $js_code . '&grant_type=' . $grant_type;

         curl_setopt($curl, CURLOPT_URL, $url);

        //  设置是否输出header
         curl_setopt($curl, CURLOPT_HEADER, false);

        //  设置是否输出结果
         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        //  设置是否检查服务器端的证书
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        //  使用curl_exec()将curl返回的结果转换成正常数据并保存到一个变量中
         $data = curl_exec($curl);

        //  关闭会话
         curl_close($curl);

        // return json_encode($data);
		return $data;
    }

}
